<?php

/* 
 * Copyright(c)2016 All rights reserved.
 * @Licenced  http://www.w3.org
 * @Author  liutian<1538731090@qq.com> liutian_jiayi
 * @Create on 2016-6-21 11:55:45
 * @Version 1.0
 */
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller{
    //获取所有用户
	public function index() {
		//实例化表模型
		$userModel = M("user");
		
		//组装where条件
		if (isset($_POST['keyword'])) {
			$keyword = trim(I("post.keyword"));
			$where = " userName LIKE '%{$keyword}%' OR nickname LIKE '%{$keyword}%' ";
		} else {
			$keyword = "";
			$where = NULL;
		}
		
		//获取分页数据
		$this->rows = $userModel->page(I("get.p", 1), C("PER_PAGE"))->where($where)->select();
		
		//获取页码栏
		$P = new \Think\Page($userModel->where($where)->count(), C("PER_PAGE"));
		$P->setConf(array(
			"FIRST" => "首页",
			"PREV" => "<",
			"NEXT" => ">",
			"LAST" => "尾页"
		));
		
		$this->show = $P->show();
		$this->keyword = $keyword;
		//显示模板
		$this->display();
	}
	
	//删除用户
	public function delete() {
		//删除一条用户记录
		$userModel = M("user");
		//执行删除
		$affected_rows = $userModel->delete(I("get.id"));//false 0 6
		//输出提示
		if ($affected_rows != false) {
			$this->success("成功删除了".$affected_rows."条记录", U("User/index"));
		} else {
			$this->error("删除失败!");
		}
	}
	
	//显示一个添加表单
	public function add() {
		$this->display();
	}
	
	//插入用户记录
	public function insert() {
		//实例化表模型
		$userModel = D("User");
		if ($userModel->create() == false) {//使用表单数据  对数据进行创建 （自动验证 自动完成）
			exit($userModel->getError());
		}
		//验证账号是否唯一
		if ($userModel->where(array("userName" => I("post.userName")))->find()) {
			$this->error("账号已被注册请重新设置！");
		}
		//添加数据入库
		if (FALSE != $insertID = $userModel->add()) {
			$this->success("添加成功，编号：".$insertID, U("User/index"), 3);
		} else {
			$this->error("添加记录失败");
		}
	}
	
	//显示编辑用户的界面
	public function edit() {
		//实力化模型
		$userModel = M("user");
		//查询该条用户记录
		$userRec = $userModel->where(array("id" => I("get.id")))->find();
		//发送数据
		$this->userRec = $userRec;
		//显示模板
		$this->display();
	}
	
	//修改用户记录
	public function update() {
		$userModel= D("User");
		if (false == $data = $userModel->create()) {
			exit($userModel->getError());
		}
		//对数据进行修改
		if ($data["password"] == md5("")) unset($data["password"]);
		
		if (false != $affected_rows = $userModel->where(array("id" => I("post.id")))->save($data)) {
			$this->success("成功修改了".$affected_rows."条记录<br />", U("User/index"));
		} else {
			$this->error("修改失败！");
		}
	}
	
	//头像上传的方法
	public function avartar() {
		//验证CSRF攻击
		if (I("post.token") != I("post.timestamp")."lfsq") {
//			echo json_encode(array("status" => false, "info" => "该文件可能来自其他站点"));
//			exit;
			$this->ajaxReturn(array("status" => false, "info" => "该文件可能来自其他站点"));
		}
		//接受表单提交过来的文件 并转存
		$file = $_FILES['Filedata'];
//		$this->ajaxReturn($file);
		//验证文件类型和大小
		
		//设置保存路劲 进行转存
		$dst = $_SERVER['DOCUMENT_ROOT']."/TP/Public/uploads/avartar/".$file['name'];
		
		//返回结果
		if ($result = move_uploaded_file($file['tmp_name'], $dst)) {
			//
			$this->ajaxReturn(array('stauts' => true, "info" => $file['name']));
		} else {
			$this->ajaxReturn(array('stauts' => false, "info" => "转存失败！"));
		}
	}
}
